package com.tuda.system.mapper;

import java.util.List;

import com.tuda.system.domain.TudaOrderTransfer;
import com.tuda.system.model.param.CarOkParam;
import com.tuda.system.model.param.EditOrderParam;
import com.tuda.system.model.param.RouteParam;
import com.tuda.system.model.result.OrderTransferResult;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 货运中转Mapper接口
 * 
 * @author tuda
 * @date 2023-07-14
 */
public interface TudaOrderTransferMapper
{

    @Select("SELECT id, order_id orderId, name, transporter, transporter_phone transporterPhone, " +
            "plate_number plateNumber, amount, owes, transfer_fee transferFee, transit_point transitPoint, refund " +
            "from tuda_order_transfer WHERE type=1 AND order_id=#{orderId} AND head_phone=#{headPhone}")
    List<TudaOrderTransfer> detailedInfoByHeadPhone(@Param("headPhone") String headPhone, @Param("orderId") Long orderId);
    @Select(" select id, order_id orderId, name, transporter, transporter_phone transporterPhone," +
            " plate_number plateNumber, amount, owes, transfer_fee transferFee, transit_point transitPoint, refund from tuda_order_transfer WHERE id=#{id}")
    TudaOrderTransfer queryTInfoById(@Param("id") Long id);

    @Select("SELECT COUNT(1) FROM tuda_order_transfer WHERE order_id=#{orderId} AND refund=0")
    int selectCount(@Param("orderId") Long orderId);

    @Select("SELECT COUNT(1) FROM tuda_order_transfer WHERE order_id=#{orderId} AND owes < 0 AND refund=0 AND type=1")
    int selectOrderTCount1(@Param("orderId") Long orderId);

    @Select("SELECT COUNT(1) FROM tuda_order_transfer WHERE order_id=#{orderId} AND owes > 0 AND refund=0 AND type=1")
    int selectOrderTCount2(@Param("orderId") Long orderId);

    @Select("SELECT id, order_id orderId, name, transit_branch transitBranch, head_phone headPhone, transporter, transporter_phone transporterPhone, plate_number plateNumber, amount, owes, transfer_fee transferFee, transit_point transitPoint," +
            "state, notes, type FROM tuda_order_transfer WHERE type=1 AND order_id=#{orderId}")
    List<TudaOrderTransfer> queryByInfo(@Param("orderId") Long orderId);

    @Select("SELECT id tId, name describes, transit_branch dest, transporter_phone phone, state FROM tuda_order_transfer WHERE type=1 AND order_id=#{orderId} ORDER BY id ASC")
    List<RouteParam> queryOrderTransferByOrderId(@Param("orderId") Long orderId);

    @Select("SELECT id, order_id orderId, name, transit_branch transitBranch, amount, transfer_fee transferFee, owes FROM tuda_order_transfer WHERE type=1 AND order_id=#{orderId} ORDER BY id ASC")
    List<OrderTransferResult> getOrderTransferList(@Param("orderId") Long orderId);

    @Select("SELECT id tId, order_id orderId, transporter, transporter_phone transporterPhone, amount, notes FROM tuda_order_transfer WHERE type=2 AND order_id=#{orderId} ORDER BY id ASC")
    List<CarOkParam> queryByOrderId(@Param("orderId") Long orderId);

    @Insert("insert into tuda_order_transfer (order_id, transporter, transporter_phone, amount, notes, type) values (#{param.orderId}, #{param.transporter}, #{param.transporterPhone}, #{param.amount},#{param.notes},#{param.type}) ")
    int save(@Param("param") TudaOrderTransfer tudaOrderTransfer);

    @Select(" select id, order_id orderId, name, transit_branch transitBranch, head_phone headPhone, transporter, transporter_phone transporterPhone," +
            " plate_number plateNumber, amount, owes, transfer_fee transferFee, transit_point transitPoint, state, notes, type from tuda_order_transfer WHERE id=#{id}")
    TudaOrderTransfer queryOrderInfoById(@Param("id") Long id);

    @Update("update tuda_order_transfer set state=2 where id=#{id}")
    int updateState(@Param("id") Long id);

    @Update("update tuda_order_transfer set refund=1, update_time=NOW() where id=#{id} and refund=0")
    int updateRefund(@Param("id") Long id);

    /**
     * 查询货运中转
     * 
     * @param id 货运中转主键
     * @return 货运中转
     */
     TudaOrderTransfer selectTudaOrderTransferById(Long id);

    /**
     * 查询货运中转列表
     * 
     * @param tudaOrderTransfer 货运中转
     * @return 货运中转集合
     */
     List<TudaOrderTransfer> selectTudaOrderTransferList(TudaOrderTransfer tudaOrderTransfer);

    /**
     * 新增货运中转
     * 
     * @param tudaOrderTransfer 货运中转
     * @return 结果
     */
     int insertTudaOrderTransfer(TudaOrderTransfer tudaOrderTransfer);

    @Insert("insert into tuda_order_transfer " +
            "(order_id, name, transit_branch, head_phone, transporter, transporter_phone, plate_number, amount, owes, transfer_fee, state, notes) " +
            "values" +
            "(#{param.orderId}, #{param.name}, #{param.transitBranch}, #{param.headPhone}, #{param.transporter}, #{param.transporterPhone}, #{param.plateNumber}, #{param.amount}, #{param.owes}, #{param.transferFee}, #{param.state}, #{param.notes})")
    int transportSave(@Param("param") EditOrderParam param);


    /**
     * 修改货运中转
     * 
     * @param tudaOrderTransfer 货运中转
     * @return 结果
     */
     int updateTudaOrderTransfer(TudaOrderTransfer tudaOrderTransfer);

     int updateTransfer(OrderTransferResult orderTransfer);

    /**
     * 删除货运中转
     * 
     * @param id 货运中转主键
     * @return 结果
     */
     int deleteTudaOrderTransferById(Long id);

    /**
     * 批量删除货运中转
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
     int deleteTudaOrderTransferByIds(String[] ids);

    @Update("update tuda_order_transfer set ${tName}=#{tValue} WHERE id=#{id}")
    int updateOrderFee(@Param("id") Long id, @Param("tName") String tName, @Param("tValue") String tValue);

}
